//
//  B_GaoDeManager.m
//  TestGaoDe
//
//  Created by 薛小刚 on 2018/9/3.
//  Copyright © 2018年 薛小刚. All rights reserved.
//

#import "B_GaoDeManager.h"
#import <AMapFoundationKit/AMapFoundationKit.h>
#import <AMapSearchKit/AMapSearchKit.h>
#import <MAMapKit/MAMapKit.h>


@interface B_GaoDeManager()

@property (nonatomic, strong) AMapLocationManager * locationManager;

@end

static B_GaoDeManager * manager;

@implementation B_GaoDeManager

+ (id) shareGaoDeManager {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manager = [[B_GaoDeManager alloc] init];
    });
    return manager;
}

+ (void) regisitWithAppKey:(NSString *) key {
    [AMapServices sharedServices].apiKey = key;
}

- (void) startLocation:(void(^)(CLLocation * location)) cLocation {
    // 带逆地理信息的一次定位（返回坐标和地址信息）
    [self.locationManager setDesiredAccuracy:kCLLocationAccuracyHundredMeters];
    //   定位超时时间，最低2s，此处设置为2s
    self.locationManager.locationTimeout =2;
    //   逆地理请求超时时间，最低2s，此处设置为2s
    self.locationManager.reGeocodeTimeout = 2;
    /// 定位回调
    [self.locationManager requestLocationWithReGeocode:YES completionBlock:^(CLLocation *location, AMapLocationReGeocode *regeocode, NSError *error) {
        if (error) {
            if (error.code == AMapLocationErrorLocateFailed) {
                return;
            }
        }
        if (regeocode) {
            NSLog(@"reGeocode:%@", regeocode);
        }
        setLocation(location);
        if (cLocation) {
            cLocation(location);
        }
    }];
}

- (AMapLocationManager *)locationManager {
    if (!_locationManager) {
        _locationManager = [[AMapLocationManager alloc] init];
    }
    return _locationManager;
}



/////地图需要v4.5.0及以上版本才必须要打开此选项（v4.5.0以下版本，需要手动配置info.plist）
//[AMapServices sharedServices].enableHTTPS = YES;
/////初始化地图
//MAMapView *_mapView = [[MAMapView alloc] initWithFrame:self.view.bounds];
//
/////把地图添加至view
//[self.view addSubview:_mapView];
//
/////如果您需要进入地图就显示定位小蓝点，则需要下面两行代码
//_mapView.showsUserLocation = YES;
//_mapView.userTrackingMode = MAUserTrackingModeFollow;


@end
